package com.xydtech.ecard.busi.service.impl;

import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.busi.domain.EcardApiBusiAndConfig;
import com.xydtech.ecard.busi.domain.EcardApiBusiReportCard;
import com.xydtech.ecard.busi.domain.EcardApiBusiReportConfig;
import com.xydtech.ecard.busi.mapper.EcardApiBusiAndConfigMapper;
import com.xydtech.ecard.busi.mapper.EcardApiBusiReportCardMapper;
import com.xydtech.ecard.busi.mapper.EcardApiBusiReportConfigMapper;
import com.xydtech.ecard.busi.service.IEcardApiBusiReportCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 电子名片业务填报绑卡类信息Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-09-26 上午9:14
 */
@Service
public class EcardApiBusiReportCardServiceImpl implements IEcardApiBusiReportCardService {

    @Autowired
    private EcardApiBusiReportCardMapper ecardBusiReportCardMapper;
    @Autowired
    private EcardApiBusiAndConfigMapper ecardBusiAndConfigMapper;
    @Autowired
    private EcardApiBusiReportConfigMapper ecardBusiReportConfigMapper;

    /**
     * 保存业务填报绑卡类数据接口
     * @param ecardBusiReportCard
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addReportCardInfo(EcardApiBusiReportCard ecardBusiReportCard, String salt) throws Exception {
        //数据加密（手机号、身份证号）
        if (!StringUtils.objIsNullOrEmpty(ecardBusiReportCard.getPerCustPhone())) {
            ecardBusiReportCard.setPerCustPhone(AESEncryptionUtils.encrypt(ecardBusiReportCard.getPerCustPhone(), salt));
        }
        if (!StringUtils.objIsNullOrEmpty(ecardBusiReportCard.getPerCustIdcard())) {
            ecardBusiReportCard.setPerCustIdcard(AESEncryptionUtils.encrypt(ecardBusiReportCard.getPerCustIdcard(), salt));
        }
        int i = ecardBusiReportCardMapper.addReportCardInfo(ecardBusiReportCard);
        /**
         * 1、先根据业务id查出业务表配置信息
         * 2、绑卡类没有存/贷类型且只有固定积分
         */
        String configIds = ecardBusiReportCard.getConfigId();
        List<EcardApiBusiAndConfig> ecardBusiAndConfigList = new ArrayList<>();
        for (String configId : configIds.split(",")) {
            EcardApiBusiReportConfig config = ecardBusiReportConfigMapper.getReportConfigById(Long.valueOf(configId));
            if (config == null) {
                throw new Exception("未配置对应积分！");
            }
            String isDpsOrLoan = config.getIsDpsOrLoan();//是否多类积分，T：是，F否
            BigDecimal points = new BigDecimal("0");//最终积分
            //绑卡类没有存/贷类型且只有固定积分
            if ("F".equals(isDpsOrLoan)){//不是存/贷只有固定积分
                Float integrationNums = config.getIntegrationNums();
                points = new BigDecimal(String.valueOf(integrationNums));
            }
            EcardApiBusiAndConfig ecardBusiAndConfig = new EcardApiBusiAndConfig();
            ecardBusiAndConfig.setBusiId(ecardBusiReportCard.getId());
            ecardBusiAndConfig.setBusiType(2);//业务填报分类：1-个人类，2-绑卡类，3-对公类，4-其他业务
            ecardBusiAndConfig.setConfigId(Long.valueOf(configId));
            ecardBusiAndConfig.setIntegralNums(points);
            ecardBusiAndConfigList.add(ecardBusiAndConfig);
        }
        //批量插入业务填报信息和业务类型关联表数据
        ecardBusiAndConfigMapper.batchAddEcardBusiAndConfigInfo(ecardBusiAndConfigList);
        return i;
    }

    /**
     * 条件查询已办理的绑卡类业务列表
     * @param ecardBusiReportCard
     * @return
     */
    @Override
    public List<EcardApiBusiReportConfig> getReportCardConfigListByCond(EcardApiBusiReportCard ecardBusiReportCard) {
        return ecardBusiReportCardMapper.getReportCardConfigListByCond(ecardBusiReportCard);
    }

    /**
     * 条件查询绑卡类业务列表
     * @param ecardBusiReportCard
     * @return
     */
    @Override
    public List<EcardApiBusiReportCard> getMyReportCardList(EcardApiBusiReportCard ecardBusiReportCard, String salt) {
        List<EcardApiBusiReportCard> ecardApiBusiReportCards = ecardBusiReportCardMapper.getMyReportCardList(ecardBusiReportCard);
        if (ecardApiBusiReportCards.size() > 0) {
            for (EcardApiBusiReportCard busiReportCard : ecardApiBusiReportCards) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(busiReportCard.getPerCustPhone())) {
                        busiReportCard.setPerCustPhone(AESEncryptionUtils.decrypt(busiReportCard.getPerCustPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(busiReportCard.getPerCustIdcard())) {
                        busiReportCard.setPerCustIdcard(AESEncryptionUtils.decrypt(busiReportCard.getPerCustIdcard(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return ecardApiBusiReportCards;
    }
}
